package com.decompiler.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 现代化按钮，具有圆角和悬停效果
 */
public class ModernButton extends JButton {
    private Color backgroundColor;
    private Color hoverColor;
    private Color pressedColor;
    private Color textColor;
    private int cornerRadius;
    private boolean isHovered = false;
    private boolean isPressed = false;
    
    /**
     * 创建一个现代化按钮
     * 
     * @param text 按钮文本
     * @param backgroundColor 背景颜色
     * @param hoverColor 悬停颜色
     * @param pressedColor 按下颜色
     * @param textColor 文本颜色
     * @param cornerRadius 圆角半径
     */
    public ModernButton(String text, Color backgroundColor, Color hoverColor, Color pressedColor, Color textColor, int cornerRadius) {
        super(text);
        this.backgroundColor = backgroundColor;
        this.hoverColor = hoverColor;
        this.pressedColor = pressedColor;
        this.textColor = textColor;
        this.cornerRadius = cornerRadius;
        
        setupButton();
    }
    
    /**
     * 创建一个使用默认颜色的现代化按钮
     * 
     * @param text 按钮文本
     */
    public ModernButton(String text) {
        this(text, new Color(59, 89, 152), new Color(68, 101, 173), new Color(48, 72, 123), Color.WHITE, 10);
    }
    
    /**
     * 创建一个使用自定义背景颜色的现代化按钮
     * 
     * @param text 按钮文本
     * @param backgroundColor 背景颜色
     */
    public ModernButton(String text, Color backgroundColor) {
        this(text, backgroundColor, 
             new Color(Math.min(backgroundColor.getRed() + 20, 255), 
                      Math.min(backgroundColor.getGreen() + 20, 255), 
                      Math.min(backgroundColor.getBlue() + 20, 255)),
             new Color(Math.max(backgroundColor.getRed() - 20, 0), 
                      Math.max(backgroundColor.getGreen() - 20, 0), 
                      Math.max(backgroundColor.getBlue() - 20, 0)),
             Color.WHITE, 10);
    }
    
    private void setupButton() {
        setFocusPainted(false);
        setBorderPainted(false);
        setContentAreaFilled(false);
        setForeground(textColor);
        setFont(new Font(getFont().getFamily(), Font.PLAIN, getFont().getSize()));
        setCursor(new Cursor(Cursor.HAND_CURSOR));
        
        // 添加鼠标事件监听器
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                isHovered = true;
                repaint();
            }
            
            @Override
            public void mouseExited(MouseEvent e) {
                isHovered = false;
                repaint();
            }
            
            @Override
            public void mousePressed(MouseEvent e) {
                isPressed = true;
                repaint();
            }
            
            @Override
            public void mouseReleased(MouseEvent e) {
                isPressed = false;
                repaint();
            }
        });
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // 根据按钮状态选择颜色
        if (isPressed) {
            g2d.setColor(pressedColor);
        } else if (isHovered) {
            g2d.setColor(hoverColor);
        } else {
            g2d.setColor(backgroundColor);
        }
        
        // 绘制圆角矩形
        g2d.fillRoundRect(0, 0, getWidth(), getHeight(), cornerRadius, cornerRadius);
        
        // 绘制文本
        FontMetrics metrics = g2d.getFontMetrics(getFont());
        int textWidth = metrics.stringWidth(getText());
        int textHeight = metrics.getHeight();
        int x = (getWidth() - textWidth) / 2;
        int y = (getHeight() - textHeight) / 2 + metrics.getAscent();
        
        g2d.setColor(textColor);
        g2d.setFont(getFont());
        g2d.drawString(getText(), x, y);
        
        g2d.dispose();
    }
    
    @Override
    public Dimension getPreferredSize() {
        Dimension size = super.getPreferredSize();
        size.width += 20; // 增加宽度，使按钮更美观
        return size;
    }
}